import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import {
	stringObject
} from "../api.js";
import {
	wxlogin
} from "../wx.js";
import {
	Http
} from "../fetch.js";
import {
	loading
} from "../global.js";
import md5 from "../md5.min.js";

const SAVEWX_KEY = md5('SAVEWX_KEY');

const store = new Vuex.Store({
	state: {
		token: '',
		loginStatus: false, //登录状态  控制  token  是否真实登录
		userInfo: false,
		schoolId: '',
		tokens: [],
		wxInfo:{}
	},
	mutations: {
		LOGIN(state, {
			token
		} = {}) {
			state.token = token || localStorage[LOGIN_KEY] || '';
			localStorage[LOGIN_KEY] = state.token;
			state.loginStatus = !!state.token;
			$http.headers["token"] = state.token;
		},
		//家长下的学生token
		TOKENS(state, tokens) {
			state.tokens = tokens || uni.getStorageSync(TOKENS_KEY) || '';
			uni.setStorageSync(TOKENS_KEY, state.tokens);
		},
		//学校ID
		SCHOOLID(state, schoolId) {
			state.schoolId = schoolId || uni.getStorageSync(SCHOOLID_KEY) || '';
			uni.setStorageSync(SCHOOLID_KEY, state.schoolId);
		},
		//保存wx信息
		SAVEWX(state, wxInfo = {}){
			state.wxInfo = {...uni.getStorageSync(SAVEWX_KEY),...wxInfo};
			uni.setStorageSync(SAVEWX_KEY, state.wxInfo);
		},
		//保存用户信息
		SAVEUSER(state, userInfo = false) {
			if (state.userInfo && userInfo) {
				userInfo = { ...state.userInfo,
					...userInfo
				};
			}
			state.userInfo = userInfo || uni.getStorageSync(INFO_KEY) || false;
			const {

			} = (state.userInfo || {});
			uni.setStorageSync(INFO_KEY, state.userInfo);
		},
		//退出
		LOGOUT(state) {
			state.token = '';
			state.tokens = null;
			state.schoolId = null;
			state.loginStatus = false;
			uni.removeStorageSync(TOKENS_KEY);
			uni.removeStorageSync(SCHOOLID_KEY);
			uni.removeStorageSync(LOGIN_KEY);
		},
		//退出
		LOGOUTUSER(state) {
			state.userInfo = false;
			state.wxInfo = null;
			uni.removeStorageSync(INFO_KEY);
			uni.removeStorageSync(SAVEWX_KEY);
		}
	},
	actions: {
		//静默登录
		async login({
			commit,
			dispatch,
			state
		}, data = {}) {
			commit('SCHOOLID');
			commit('LOGIN', data);
			commit("SAVEUSER");
			commit("SAVEWX");
			//token不存在
			if (!state.token && !state.wxInfo.openid) {
				return wxlogin().then(res => {
					//学校ID
					commit('SCHOOLID', res.state || '');
					if (res.code === 200) {
						commit('TOKENS', res.result);
						commit('LOGIN', {
							token: res.result[0]
						});
						history.replaceState(null, '', '/#/pages/index/index');
					} else {
						commit("SAVEWX", res);
						history.replaceState(null, '', '/#/pages/login/login');
					}
				});
			}else if(!state.token && state.wxInfo.openid){
				history.replaceState(null, '', '/#/pages/login/login');
			}
			return '';
		},
		//静默退出
		async logout({
			commit
		}, stauts) {
			commit("LOGOUT");
			!stauts && commit('LOGOUTUSER');
			return '';
		},
		//获取学生信息
		getInfo({
			commit
		}) {
			return $http.getInfo().then(res => {
				const i = { ...res,
					studentId: res.id
				};
				commit('SAVEUSER', i);
				return i;
			});
		}
	}
});
const $http = new Http({
	$store: store
});
window.$http = $http;
Vue.prototype.$http = $http;
Vue.prototype.$loading = loading;
const LOGIN_KEY = md5('LOGIN_KEY' + $http.type || ''),
	INFO_KEY = md5("INFO_KEY" + $http.type || ''),
	SCHOOLID_KEY = md5('SCHOOLID' + $http.type || ''),
	TOKENS_KEY = md5('TOKENS' + $http.type || '');

export default store
